#!/usr/bin/env python
#############################################################################
#                                                                           #
#   File: win-patch.py                                                      #
#                                                                           #
#   Copyright (C) 2010-2011                                                 #
#                                                                           #
#   Home: http://code.google.com/p/you-proxy/                               #
#                                                                           #
#   This file is part of wallproxy-fixed.                                   #
#                                                                           #
#   Thanks to wallproxy. This software fixed some bug in wallproxy 1.0.4    #
#                                                                           #
#   wallproxy-fixed is free software: you can redistribute it and/or modify #
#   it under the terms of the GNU General Public License as                 #
#   published by the Free Software Foundation, either version 3 of the      #
#   License, or (at your option) any later version.                         #
#                                                                           #
#   wallproxy-fixed is distributed in the hope that it will be useful,      #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#   GNU General Public License for more details.                            #
#                                                                           #
#   You should have received a copy of the GNU General Public License       #
#   along with wallproxy-fixed.  If not, see <http://www.gnu.org/licenses/>.#
#                                                                           #
#############################################################################

__author__ = 'd3d3LmVodXN0QGdtYWlsLmNvbQ=='.decode('base64')
__version__ = '0.0.1'

import sys, os, re
dll = 'python%s%s.dll' % (sys.version[0], sys.version[2])
dir = os.path.dirname(sys.argv[0])
files = [os.path.join(dir, 'OpenSSL/crypto.pyd')]
dir = os.path.join(dir, 'Crypto/Cipher')
for fn in os.listdir(dir):
    if fn.endswith('.pyd'):
        files.append(os.path.join(dir, fn))
for fn in files:
    print fn,
    try:
        fp = open(fn, 'rb')
        data = fp.read()
        fp.close()
        newdata = re.sub(r'python\d{2}\.dll', dll, data, 1)
        if newdata != data:
            try:
                os.rename(fn, fn+'.bak')
            except:
                print 'backup failed',
            fp = open(fn, 'wb')
            fp.write(newdata)
            fp.close()
            print 'done'
        else:
            print 'pass'
    except IOError, e:
        print e
